﻿using System.Net.Http;
using System.Net.Http.Headers;
using Flurl;
using Newtonsoft.Json;
using ZilLion.Core.Unities.UnitiesMethods;
using ZilLion.TaskManager.Entity.Config;
using ZilLion.TaskManager.Entity.Entity;
using ZilLion.TaskManager.Entity.Response;

namespace ZilLion.TaskManager.Repository
{
    public class TaskStateRespository : ITaskStateRespository
    {
        public TaskStateResponse GetTaskStateById(string taskid, string nodeid = null)
        {
            var url = Url.Combine(TaskManagerGlobalSetting.GetInstance().ManagerServiceHost, "Api",
                "TaskPlatform",
                "TaskState", "GetTaskState"
            );
            return HttpHelper.HttpRequestGet<TaskStateResponse>($"{url}?taskid={taskid}&nodeid={nodeid} ");
        }

        public TaskStateResponse GetAllTaskState(string nodeid)
        {
            var combine =
                Url.Combine(TaskManagerGlobalSetting.GetInstance().ManagerServiceHost, "Api",
                    "TaskPlatform",
                    "TaskState", "GetAllTaskState");
            var url =
                $"{combine}?nodeid={nodeid}";


            return HttpHelper.HttpRequestGet<TaskStateResponse>($"{url} ");
        }


        public void UpdateTaskState(TaskStateEntity stateEntity)
        {
            var url = Url.Combine(TaskManagerGlobalSetting.GetInstance().ManagerServiceHost, "Api",
                "TaskPlatform",
                "TaskState", "UpdateTaskState"
            );


            var httpcontent = new StringContent(JsonConvert.SerializeObject(stateEntity))
            {
                Headers = {ContentType = new MediaTypeHeaderValue("application/json")}
            };


            HttpHelper.HttpRequestPost<ResponseBase>($"{url} ", httpcontent);
        }

        public void ReportTaskExcuted(TaskStateEntity stateEntity)
        {
            var url = Url.Combine(TaskManagerGlobalSetting.GetInstance().ManagerServiceHost, "Api",
                "TaskPlatform",
                "TaskState", "ReportTaskExcuted"
            );


            var httpcontent = new StringContent(JsonConvert.SerializeObject(stateEntity))
            {
                Headers = {ContentType = new MediaTypeHeaderValue("application/json")}
            };


            HttpHelper.HttpRequestPost<ResponseBase>($"{url} ", httpcontent);
        }

        public void ReportTaskError(string taskid, string nodeid)
        {
            var url = Url.Combine(TaskManagerGlobalSetting.GetInstance().ManagerServiceHost, "Api",
                "TaskPlatform",
                "TaskState", "ReportTaskError"
            );
            HttpHelper.HttpRequestGet<ResponseBase>($"{url}?taskid={taskid}&nodeid={nodeid}");
        }


        public void ReportCommandExcuted(TaskConmmandEntity command)
        {
            var url = Url.Combine(TaskManagerGlobalSetting.GetInstance().ManagerServiceHost, "Api",
                "TaskPlatform",
                "TaskState", "ReportCommandExcuted"
            );
            var httpcontent = new StringContent(JsonConvert.SerializeObject(command))
            {
                Headers = {ContentType = new MediaTypeHeaderValue("application/json")}
            };


            HttpHelper.HttpRequestPost<ResponseBase>($"{url} ", httpcontent);
        }
    }
}